<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打开一个 PostgreSQL 连接</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pg-close.html">pg_close</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.pg-connection-busy.html">pg_connection_busy</a></div>
 <div class="up"><a href="ref.pgsql.html">PostgreSQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.pg-connect" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">pg_connect</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">pg_connect</span> &mdash; <span class="dc-title">打开一个 PostgreSQL 连接</span></p>

   </div>
   <div class="refsect1 unknown-seealsp" id="refsect1-function.pg-connect-unknown-seealsp">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="type">resource</span> <span class="methodname"><strong>pg_connect</strong></span>
      ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$connection_string</code></span>
     )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>pg_connect()</strong></span> 返回其它 PostgreSQL 函数所需要的资源。
    </p>
    <p class="para">
     <span class="function"><strong>pg_connect()</strong></span> 打开一个由
     <em><code class="parameter">connection_string</code></em> 所指定的
     PostgreSQL 数据库的连接。如果成功则返回连接资源，如果不能连接则返回
     <strong><code>FALSE</code></strong>。<em><code class="parameter">connection_string</code></em> 应该是用引号引起来的字符串。
     <div class="example" id="example-2016">
      <p><strong>Example #1 使用 <span class="function"><strong>pg_connect()</strong></span></strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbconn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=mary"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect&nbsp;to&nbsp;a&nbsp;database&nbsp;named&nbsp;"mary"<br /></span><span style="color: #0000BB">$dbconn2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=localhost&nbsp;port=5432&nbsp;dbname=mary"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;connect&nbsp;to&nbsp;a&nbsp;database&nbsp;named&nbsp;"mary"&nbsp;on&nbsp;"localhost"&nbsp;at&nbsp;port&nbsp;"5432"<br /></span><span style="color: #0000BB">$dbconn3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=sheep&nbsp;port=5432&nbsp;dbname=mary&nbsp;user=lamb&nbsp;password=foo"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect&nbsp;to&nbsp;a&nbsp;database&nbsp;named&nbsp;"mary"&nbsp;on&nbsp;the&nbsp;host&nbsp;"sheep"&nbsp;with&nbsp;a&nbsp;username&nbsp;and&nbsp;password<br /><br /></span><span style="color: #0000BB">$conn_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"host=sheep&nbsp;port=5432&nbsp;dbname=test&nbsp;user=lamb&nbsp;password=bar"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dbconn4&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn_string</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect&nbsp;to&nbsp;a&nbsp;database&nbsp;named&nbsp;"test"&nbsp;on&nbsp;the&nbsp;host&nbsp;"sheep"&nbsp;with&nbsp;a&nbsp;username&nbsp;and&nbsp;password<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
     <em><code class="parameter">connection_string</code></em> 所包括的参数有
     <em><code class="parameter">host</code></em>，<em><code class="parameter">port</code></em>，<em><code class="parameter">tty</code></em>, <em><code class="parameter">options</code></em>，<em><code class="parameter">dbname</code></em>, <em><code class="parameter">user</code></em>
     和 <em><code class="parameter">password</code></em>。
    </p>
    <p class="para">
     如果用同样的 <em><code class="parameter">connection_string</code></em> 再次调用
     <span class="function"><strong>pg_connect()</strong></span>，不会建立新连接，而是返回前面已经打开的连接资源。如果使用不同的连接字符串，则可以和同一个数据库建立多个连接。
    </p>
    <p class="para">
     旧的多参数语法 <strong class="command">$conn = pg_connect(&quot;host&quot;, &quot;port&quot;, &quot;options&quot;, &quot;tty&quot;, &quot;dbname&quot;)</strong> 已经不提倡使用。
    </p>
    <p class="para">
     参见 <span class="function"><a href="function.pg-pconnect.html" class="function">pg_pconnect()</a></span>，<span class="function"><a href="function.pg-close.html" class="function">pg_close()</a></span>，<span class="function"><a href="function.pg-host.html" class="function">pg_host()</a></span>，<span class="function"><a href="function.pg-port.html" class="function">pg_port()</a></span>, <span class="function"><a href="function.pg-tty.html" class="function">pg_tty()</a></span>，<span class="function"><a href="function.pg-options.html" class="function">pg_options()</a></span>
     和 <span class="function"><a href="function.pg-dbname.html" class="function">pg_dbname()</a></span>。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pg-close.html">pg_close</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.pg-connection-busy.html">pg_connection_busy</a></div>
 <div class="up"><a href="ref.pgsql.html">PostgreSQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
